<?php
/*
//发送方式mail_type的值

close 关闭邮件发送
smtp  通过SMTP SOCKET 连接 SMTP 服务器发送(支持ESMTP验证)
mail  通过PHP mail 函数发送(通常为Unix/Linux 主机)
psmtp 通过PHP mail 函数SMTP发送(通常为WIN主机)

//邮件头的分隔符mail_delimiter的值
1 邮件头的分隔符 	使用 CRLF 作为分隔符(通常为Windows主机)
2 使用 LF 作为分隔符(通常为Unix/Linux主机)
3 使用 CR 作为分隔符(通常为Mac主机)
*/
class send_mail{
	private $DT=null;
	private $testmail=0;
	public function __construct($config,$testmail=0){
		$this->DT=array(
			'charset'=>'utf-8',
			'sitename'=>'',
			'mail_name'=>'',//发件人名称 系统发送的信件将显示此名称，不填则显示网站名
			'mail_log'=>'0',
			'mail_sender'=>'',//发件人邮箱 系统发送的信件将以此邮箱名义发送
			'mail_sign'=>'',//邮件签名
			'mail_delimiter'=>2,
			'mail_type'=>'close',
			'smtp_host'=>'',//SMTP服务器 smtp.126.com
			'smtp_port'=>'25',//SMTP端口
			'smtp_auth'=>'1',//SMTP服务器是否验证
			'smtp_user'=>'',//邮箱帐号
			'smtp_pass'=>'',//邮箱密码
			
		);
		$this->DT=array_merge($this->DT,$config);
		$this->testmail=$testmail;
	}
	private function dalert($msg){
		alert($msg);
		back();
	}
	private function log_write($msg,$t){
		return true;
	}
	public function dmail($mail_to, $mail_subject, $mail_body, $mail_from = '', $mail_sign = true) {
		$DT=$this->DT;
		if($DT['mail_type'] == 'close') return 'close';
		$sendmail_from = $mail_from ? $mail_from : $DT['mail_sender'];
		$mail_from = "=?".strtolower($DT['charset'])."?B?".base64_encode($DT['mail_name'] ? $DT['mail_name'] : $DT['sitename'])."?= <".$sendmail_from.">";
		$mail_subject = stripslashes($mail_subject);
		$mail_subject = str_replace("\r", '', str_replace("\n", '', $mail_subject));
		$mail_subject = "=?".strtolower($DT['charset'])."?B?".base64_encode($mail_subject)."?=";
		if($DT['mail_sign'] && $mail_sign) $mail_body .= $DT['mail_sign'];
		$mail_body = stripslashes($mail_body);
		$mail_body = chunk_split(base64_encode(str_replace("\r\n.", " \r\n..", str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $mail_body)))))));
		$mail_dlmt = $DT['mail_delimiter'] == 1 ? "\r\n" : ($DT['mail_delimiter'] == 2 ? "\n" : "\r");
		$headers = '';
		$headers .= "From: $mail_from".$mail_dlmt;
		$headers .= "X-Priority: 3".$mail_dlmt;
		$headers .= "X-Mailer: demo".$mail_dlmt;
		$headers .= "MIME-Version: 1.0".$mail_dlmt;
		$headers .= "Content-type: text/html; charset=".$DT['charset'].$mail_dlmt;
		$headers .= "Content-Transfer-Encoding: base64".$mail_dlmt;
		if($DT['mail_type'] == 'smtp') {
			$host = $DT['smtp_host'].':'.$DT['smtp_port'].' ';
			if(!$fp = fsockopen($DT['smtp_host'], $DT['smtp_port'], $errno, $errstr, 30)) {
				$errmsg = $host.'can not connect to the SMTP server';
				if($this->testmail) $this->dalert('Error:'.trim($errmsg));
				$this->log_write($errmsg, 'smtp');
				return $errmsg;
			}
			stream_set_blocking($fp, true);
			$RE = fgets($fp, 512);
			if(substr($RE, 0, 3) != '220') {
				$errmsg = $host.$RE;
				if($this->testmail) $this->dalert('Error:'.trim($errmsg));
				$this->log_write($errmsg, 'smtp');
				return $errmsg;
			}
			fputs($fp, ($DT['smtp_auth'] ? 'EHLO' : 'HELO')." demo\r\n");
			$RE = fgets($fp, 512);
			if(substr($RE, 0, 3) != 220 && substr($RE, 0, 3) != 250) {
				$errmsg = $host.'HELO/EHLO - '.$RE;
				if($this->testmail) $this->dalert('Error:'.trim($errmsg));
				$this->log_write($errmsg, 'smtp');
				return $errmsg;
			}
			while(1) {
				if(substr($RE, 3, 1) != '-' || empty($RE)) break;
				$RE = fgets($fp, 512);
			}
			if($DT['smtp_auth']) {
				fputs($fp, "AUTH LOGIN\r\n");
				$RE = fgets($fp, 512);
				if(substr($RE, 0, 3) != 334) {
					$errmsg = $host.'AUTH LOGIN - '.$RE;
					if($this->testmail) $this->dalert('Error:'.trim($errmsg));
					$this->log_write($errmsg, 'smtp');
					return $errmsg;
				}
				fputs($fp, base64_encode($DT['smtp_user'])."\r\n");
				$RE = fgets($fp, 512);
				if(substr($RE, 0, 3) != 334) {
					$errmsg = $host.'USERNAME - '.$RE;
					if($this->testmail) $this->dalert('Error:'.trim($errmsg));
					$this->log_write($errmsg, 'smtp');
					return $errmsg;
				}
				fputs($fp, base64_encode($DT['smtp_pass'])."\r\n");
				$RE = fgets($fp, 512);
				if(substr($RE, 0, 3) != 235) {
					$errmsg = $host.'PASSWORD - '.$RE;
					if($this->testmail) $this->dalert('Error:'.trim($errmsg));
					$this->log_write($errmsg, 'smtp');
					return $errmsg;
				}
				$mail_from = strpos($DT['smtp_user'], '@') !== false ? $DT['smtp_user'] : $DT['mail_sender'];
			} else {
				$mail_from = $DT['mail_sender'];
			}
			fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $mail_from).">\r\n");
			$RE = fgets($fp, 512);
			if(substr($RE, 0, 3) != 250) {
				fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $mail_from).">\r\n");
				$RE = fgets($fp, 512);
				if(substr($RE, 0, 3) != 250) {
					$errmsg = $host.'MAIL FROM - '.$RE;
					if($this->testmail) $this->dalert('Error:'.trim($errmsg));
					$this->log_write($errmsg, 'smtp');
					return $errmsg;
				}
			}
			foreach(explode(',', $mail_to) as $touser) {
				$touser = trim($touser);
				if($touser) {
					fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n");
					$RE = fgets($fp, 512);
					if(substr($RE, 0, 3) != 250) {
						fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n");
						$RE = fgets($fp, 512);
						$errmsg = $host.'RCPT TO - '.$RE;
						if($this->testmail) $this->dalert('Error:'.trim($errmsg));
						$this->log_write($errmsg, 'smtp');
						return $errmsg;
					}
				}
			}
			fputs($fp, "DATA\r\n");
			$RE = fgets($fp, 512);
			if(substr($RE, 0, 3) != 354) {
				$errmsg = $host.'DATA - '.$RE;
				if($this->testmail) $this->dalert('Error:'.trim($errmsg));
				$this->log_write($errmsg, 'smtp');
				return $errmsg;
			}
			list($msec, $sec) = explode(' ', microtime());
			$headers .= "Message-ID: <".date('YmdHis', $sec).".".($msec*1000000).".".substr($mail_from, strpos($mail_from,'@')).">".$mail_dlmt;
			fputs($fp, "Date: ".date('r')."\r\n");
			fputs($fp, "To: ".$mail_to."\r\n");
			fputs($fp, "Subject: ".$mail_subject."\r\n");
			fputs($fp, $headers."\r\n");
			fputs($fp, "\r\n\r\n");
			fputs($fp, "$mail_body\r\n.\r\n");
			$RE = fgets($fp, 512);
			if(substr($RE, 0, 3) != 250) {
				$errmsg = $host.'END - '.$RE;
				if($this->testmail) $this->dalert('Error:'.trim($errmsg));
				$this->log_write($errmsg, 'smtp');
				return $errmsg;
			}
			fputs($fp, "QUIT\r\n");
			return 'SUCCESS';
		} else {
			if($DT['mail_type'] != 'mail') {
				ini_set('SMTP', $DT['smtp_host']);
				ini_set('smtp_port', $DT['smtp_port']);
				ini_set('sendmail_from', $sendmail_from);
			}
			return @mail($mail_to, $mail_subject, $mail_body, $headers) ? 'SUCCESS' : '';
		}
	}
	//外部调用方法 send($email, $title, stripslashes($content));
	function send($mail_to, $mail_subject, $mail_body, $mail_from = '', $mail_sign = true) {
		$DT=$this->DT;
		$result = $this->dmail(trim($mail_to), $mail_subject, $mail_body, $mail_from, $mail_sign);
		$success = $result == 'SUCCESS' ? 1 : 0;
		return $success;
	}
//	
}